package com.ruoyi.elasticsearch.canal;

import com.alibaba.fastjson.JSON;
import com.ruoyi.elasticsearch.domain.Hotel;
import com.ruoyi.elasticsearch.es.domain.HotelEs;
import com.ruoyi.elasticsearch.es.mapper.HotelEsMapper;
import com.ruoyi.elasticsearch.es.service.HotelEsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import top.javatool.canal.client.annotation.CanalTable;
import top.javatool.canal.client.handler.EntryHandler;

//@Component
//@CanalTable(value = "tb_hotel")
public class HotelHandler implements EntryHandler<Hotel> {

    //@Autowired
    private HotelEsService hotelEsService;
    /***
     * 增加数据
     * @param hotel
     */
    @Override
    public void insert(Hotel hotel) {
        System.out.println("===========insert:"+hotel);
        //将hotel转换成hotelEs
        HotelEs hotelEs = JSON.parseObject( JSON.toJSONString(hotel) , HotelEs.class);
        //同步索引
        hotelEsService.modify(1,hotelEs);
    }

    /***
     * 修改数据
     * @param before
     * @param after
     */
    @Override
    public void update(Hotel before, Hotel after) {
        System.out.println("===========update-after:"+JSON.toJSONString(after));
        //将hotel转换成hotelEs
        HotelEs hotelEs = JSON.parseObject( JSON.toJSONString(after) , HotelEs.class);
        //同步索引
        hotelEsService.modify(2,hotelEs);
    }

    /***
     * 删除数据
     * @param sku
     */
    @Override
    public void delete(Hotel sku) {
        System.out.println("===========delete:"+sku);
    }
}
